package com.itcoon.system.web.controller;

import com.itcoon.cloud.framework.mybatis.core.Page;
import com.itcoon.cloud.framework.page.IPageResult;
import com.itcoon.cloud.framework.transform.Transformer;
import com.itcoon.cloud.framework.webmvc.request.ApiRequest;
import com.itcoon.cloud.framework.webmvc.request.SortPage;
import com.itcoon.cloud.framework.webmvc.response.ApiPageResponse;
import com.itcoon.cloud.framework.webmvc.response.ApiResponse;
import com.itcoon.system.entity.AdminUserDO;
import com.itcoon.system.service.AdminUserService;
import com.itcoon.system.web.vo.AdminUserCreateReqVO;
import com.itcoon.system.web.vo.AdminUserRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.util.List;

/**
 *
 *
 * @author Athric Yu
 * 2024/12/20
 **/
@Tag(name = "用户")
@RequestMapping("/users")
@RequiredArgsConstructor
@RestController
public class AdminUserController {

    private final AdminUserService adminUserService;

    @PostMapping
    @Operation(summary = "注册")
    @PermitAll
    public ApiResponse<Void> register(@RequestBody @Validated ApiRequest<AdminUserCreateReqVO> request){
        adminUserService.register(request.getCommand());
        return ApiResponse.ok();
    }

    @GetMapping
    @Operation(summary = "分页查询用户")
    public ApiPageResponse<List<AdminUserRespVO>> searchPage(@Validated SortPage sortPage){
        Page<AdminUserDO> page = adminUserService.page(Page.of(sortPage));
        IPageResult<AdminUserRespVO> result = Transformer.to(IPageResult.class).apply(page).convert();
        return ApiPageResponse.ok(result);
    }
}
